﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LiquidMeasure.Properties;
using System.Globalization;
using System.Reflection;
using Microsoft.Win32;
using System.IO;
using Utility;

namespace LiquidMeasure
{
    /// <summary>
    /// Interaction logic for CalibrationWindow.xaml
    /// </summary>
    public partial class CalibrationWindow : Window
    {
        MyVisualHost visualHost;
        CalibStage curStage = CalibStage.Init;
        
        public CalibrationWindow()
        {
            InitializeComponent();
            visualHost = new MyVisualHost();
            SetStage(CalibStage.High);
            myCanvas.Children.Add(visualHost);
            visualHost.onPositionChanged += new MyVisualHost.PositionChanged(visualHost_onPositionChanged);
        }

        private void SetStage(CalibStage calibStage)
        {
            curStage = calibStage;
            visualHost.Stage = curStage;
            myCanvas.InvalidateVisual();
            if (calibStage == CalibStage.Low)
                btnNext.Content = "完成";
            SetHint();
        }

        void visualHost_onPositionChanged()
        {
            this.Dispatcher.BeginInvoke(
              (Action)delegate()
              {
                  //Update code goes in here
                  myCanvas.InvalidateVisual();
              });
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            string errMsg = "";
            bool bok = true;

            bok = CheckValue(txtHeight.Text, ref errMsg);
            if (!bok)
            {
                SetInfo("高度值设置错误：" + errMsg, Brushes.Red);
                return;
            }

            bok = CheckValue(txtVolume.Text, ref errMsg);
            if (!bok)
            {
                SetInfo("体积值设置错误：" + errMsg, Brushes.Red);
                return;
            }

            if (curStage == CalibStage.High)
            {
                Calib.Instance.highHeight = double.Parse(txtHeight.Text);
                Calib.Instance.highVolume = double.Parse(txtVolume.Text);
                txtHeight.Text = Calib.Instance.lowHeight.ToString();
                txtVolume.Text = Calib.Instance.lowVolume.ToString();

                SetStage(CalibStage.Low);
            }
            else
            {
                SaveCalibResult();
                this.Close();
            }
        }

        static public string GetCalibFolder()
        {
            return Calib.GetCalibFolder();
        }

        private void SaveCalibResult()
        {
            Calib.Instance.lowHeight = double.Parse(txtHeight.Text);
            Calib.Instance.lowVolume = double.Parse(txtVolume.Text);
            List<double> highMarkers = visualHost.GetVisualHeights(stringRes.HighMarker);
            List<double> lowMarkers = visualHost.GetVisualHeights(stringRes.LowMarker);
            List<MARKER_POS> markerPositions = new List<MARKER_POS>();
            for (int i = 0; i < highMarkers.Count; i++)
            {
                markerPositions.Add(new MARKER_POS(highMarkers[i],lowMarkers[i]));
            }
            Calib.Instance.calibPositions = markerPositions;
            List<ROI> rois = (List<ROI>)visualHost.GetVisualROIs("ROI");
            Calib.Instance.roiList = rois;
            string sSaveFile = GetCalibFolder() + stringRes.DefaultCalib;
            SerializeHelper.SaveSettings(Calib.Instance, sSaveFile);
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.InitialDirectory = GetCalibFolder();
            dlg.Filter = "xml files (*.xml)|*.xml";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CheckFileExists = false;
            bool? result = dlg.ShowDialog();
            
            string sResultFile = "";
            if( result == true)
                sResultFile = dlg.FileName;
            Calib.Instance.Write2ROI4Cpp();
            if (sSaveFile == sResultFile)
                return;
            File.Copy(sSaveFile, sResultFile,true);
        }

        
    

        private void SetInfo(string errMsg, Brush color)
        {
            //throw new NotImplementedException();
            txtHint.Text = errMsg;
            txtHint.Foreground = color;
        }

       
        private bool CheckValue(string s, ref string tmpMsg)
        {
            if (s == string.Empty)
            {
                tmpMsg = "字符串为空！";
                return false;
            }

            double res;
            if (!double.TryParse(s, out res))
            {
                tmpMsg = "不是数字！";
                return false;
            }

            return true;
        }

      

        private void SetHint()
        {
            string sHint = stringRes.HighStageHint;
            if (curStage == CalibStage.Low)
            {

                sHint = stringRes.LowStageHint;
            }
            txtHint.Foreground = Brushes.Black;
            txtHint.Text = sHint;
        }

       
    }

  
}
